package com.laotou99.wmsjjj.wmsjjj.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.laotou99.wmsjjj.wmsjjj.bean.vo.ASNDetailVO;
import com.laotou99.wmsjjj.wmsjjj.bean.vo.ASNVO;
import com.laotou99.wmsjjj.wmsjjj.service.ASNService;

@Controller
@RequestMapping("/asn")
public class ASNController {
	
	@Autowired
	ASNService asnService;

	//测试
	/**
	 * @category 测试
	 * */
	@RequestMapping("/getTest1")
	@ResponseBody
	public String getTest1() 
	{
		//test11();
		//test12();
		return "test1";
	}
	

	@RequestMapping("/getASNList")
	@ResponseBody
	public List<ASNVO> getASNList()
	{ 
		List<ASNVO> li1 = asnService.getASNList(); 
		return li1;
	}
	
	@RequestMapping("/getASNDetailList")
	@ResponseBody
	public List<ASNDetailVO> 
		getASNDetailList(long id1)
	{ 	
		List<ASNDetailVO> li2 = 
			asnService.getASNDetailList(id1);
		
		return li2;
	}
	

	@RequestMapping("/asnquery")
	public String asnquery(){ 
		return "asn/asnquery";
	}
	
	@RequestMapping("/asnlist")
	public String asnlist(){ 
		return "asn/asnlist";
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/getASNListDetail1")
	@ResponseBody
	public String getASNListDetail1(HttpServletRequest request){
		System.out.println("getASNListDetail1");
		String asnNum = request.getParameter("asnNum");
		System.out.println("asnNum-=>"+JSON.toJSONString(asnNum)); 
		
		if(asnNum.equals("")){
			return "";
		}

		List li1 = new ArrayList<>();
		for(int i1 = 0;i1<10;i1++){ 
			Map m2 = new HashMap<>();
			m2.put("ASNDNum", asnNum+"-"+i1);
			m2.put("SKUID", "340081"+i1);
			m2.put("SKUCN", "测试名称"+i1);
			m2.put("VEDCODE", "A098302918"+i1);
			m2.put("UOMNUM", "331"+i1);
			m2.put("FDUNIT", "个");
			m2.put("LOT", "还未入库");
			m2.put("LOC", "还未入库");
			m2.put("remark1", "配件"+i1);
			li1.add(m2); 
		}
		Map m3 = new HashMap<>();
		m3.put("total", li1.size());
		m3.put("rows", li1);
		return JSON.toJSONString(m3);
	}
	
	 
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/getASNList11")
	@ResponseBody
	public String getASNList11(){
		System.out.println("getASNList1()");

		List li1 = new ArrayList<>();
		for(int i1 = 0;i1<10;i1++){ 
			Map m2 = new HashMap<>();
			m2.put("ASNNum", "ASN0034234123"+i1);
			m2.put("StoreKey", "00987");
			m2.put("CreateDate", "2017-12-25 13:33:32");
			m2.put("ASNState", "未收货");
			m2.put("INVNo", "90888766547765"+i1);
			m2.put("BUSINO", "123453234"+i1);
			m2.put("PKGType", "托盘+纸盒");
			m2.put("REVTime", "无");
			m2.put("remark1", "电脑配件");
			li1.add(m2); 
		}
		Map m3 = new HashMap<>();
		m3.put("total", li1.size());
		m3.put("rows", li1);
		return JSON.toJSONString(m3);
	}
	
	
	//得到所有的ASN列表
	@RequestMapping("/getASNList1")
	@ResponseBody
	public String getASNList1()
	{ 
		List<ASNVO> li1 = asnService.getASNList(); 
//		String str1 = JSON.toJSONString(li1);
//		System.out.println("str1-=>"+str1);
		return JSONObject.toJSONString(li1);
	}
	
	//根据ID得到当前ASN的列表
	@RequestMapping("/getASNListById")
	@ResponseBody
	public ASNVO getASNListById(long asnid) { 
		ASNVO an1 = null;
		if(asnid==0) {
			return an1;
		}
		an1 = asnService.getAsnById(asnid); 
		return an1;
	}
	
	//保存一条ASN的DETAIL的记录 ，保存一条ASN明细记录
	@RequestMapping("/saveASNDetail")
	@ResponseBody
	public String saveASNDetail(
		ASNDetailVO asndvo,long id1) 
	{
		if(id1==0) {}else {
			if(asnService.saveASNDetail(asndvo,id1)) {
				return "OK";
			}
		}
		return "ERROR";
	}
	
	//保存一条ASN记录，可以新增或是更新
	@RequestMapping("/saveASN")
	@ResponseBody
	public String saveASN(ASNVO asnvo) {
		System.out.println("asnvo-=>"
			+JSON.toJSONString(asnvo));		
		if(asnvo.getId()==0) {
			if(asnService.saveASN(asnvo)) {
				return "OK";
			}
		}else{
			if(asnService.updateASN(asnvo)) {
				return "OK";
			}
		}
		return "ERROR";
	}
	
	//一条ASN记录，收货操作
	@RequestMapping("/receiveASN")
	@ResponseBody
	public String receiveASN(Long id1) { 
		String str1 = asnService.receiveASN(id1); 
		return str1;
	}
	
	//一条ASN记录，取消收货操作
	@RequestMapping("/canncelRecASN")
	@ResponseBody
	public String canncelRecASN(Long id1) { 
		
		return "";
	}

	//预分配库位
	@RequestMapping("/preAllocASN")
	@ResponseBody
	public String preAllocASN(long id1) {
		
		
		return "";
	}
	
	//删除ASN
	@RequestMapping("/deleteASN")
	@ResponseBody
	public String deleteASN(long id1) {
		
		return "";
	}
	
	//删除ASN明细
	@RequestMapping("/deleteASNDetail")
	@ResponseBody
	public String deleteASNDetail(
		long id1,long detailID1) 
	{
		
		return "";
	}
	
	private void test12() 
	{
		ASNDetailVO dvo1 = new ASNDetailVO();
		dvo1.setSku("0001");
		dvo1.setMoqNum(22);
		asnService.saveASNDetail(dvo1, 1l);
		dvo1 = new ASNDetailVO();
		dvo1.setSku("0002");
		dvo1.setMoqNum(51);
		asnService.saveASNDetail(dvo1, 2l);
		dvo1 = new ASNDetailVO();
		dvo1.setSku("0003");
		dvo1.setMoqNum(42);
		asnService.saveASNDetail(dvo1, 3l);
		dvo1 = new ASNDetailVO();
		dvo1.setSku("0011");
		dvo1.setMoqNum(34);
		asnService.saveASNDetail(dvo1, 1l);
		dvo1 = new ASNDetailVO();
		dvo1.setSku("0012");
		dvo1.setMoqNum(32);
		asnService.saveASNDetail(dvo1, 2l);
		dvo1 = new ASNDetailVO();
		dvo1.setSku("0013");
		dvo1.setMoqNum(21);
		asnService.saveASNDetail(dvo1, 3l);
		dvo1 = new ASNDetailVO();
		dvo1.setSku("0021");
		dvo1.setMoqNum(55);
		asnService.saveASNDetail(dvo1, 1l);
		dvo1 = new ASNDetailVO();
		dvo1.setSku("0022");
		dvo1.setMoqNum(44);
		asnService.saveASNDetail(dvo1, 2l);
		dvo1 = new ASNDetailVO();
		dvo1.setSku("0023");
		dvo1.setMoqNum(33);
		asnService.saveASNDetail(dvo1, 3l);
	}

	private void test11() 
	{
		ASNVO asnvo = new ASNVO();
		asnvo.setStorekey("aaaa1");
		asnService.saveASN(asnvo);
		asnvo = new ASNVO();
		asnvo.setStorekey("aaaa2");
		asnService.saveASN(asnvo);
		asnvo = new ASNVO();
		asnvo.setStorekey("aaaa3");
		asnService.saveASN(asnvo);
		
	}

}
